package com.baizhi.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baizhi.entity.Record;
import com.baizhi.service.IRecordService;
import com.baizhi.util.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhenggang
 * @since 2021-09-29
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private IRecordService rs;
    @GetMapping("selectByStuId/{stuId}")
    public List<Record> selectByStuId(@PathVariable("stuId") int stuId){
        QueryWrapper<Record> wrapper=new QueryWrapper();
        wrapper.eq("stuId",stuId);
        return rs.list(wrapper);
    }
    @GetMapping("selectByPage")
    public PageInfo<Record> selectByPage(int pageNo,int pageSize,String name,String stuName){
        return rs.selectByPage(pageNo,pageSize,name,stuName);
    }
    @PostMapping("insert")
    //requestHeader表示从请求头里面获取数据。从请求头里面获取key为Authorization的数据
    public void insert(@RequestBody Record record,@RequestHeader("Authorization") String token){
        //插入前给record的创建人和创建时间复制
        // 后期要从登陆人里面获取信息
        DecodedJWT verify = JwtUtils.verify(token);
        String username = verify.getClaim("username").asString();
        record.setCreateBy(username);//要从登陆人信息里面获取
        //LocalDateTime.now()获取到系统时间
        record.setCreateDate(LocalDateTime.now());
        rs.save(record);
    }
    @PutMapping("update")
    public void update(@RequestBody Record record){
        rs.updateById(record);
    }
    @GetMapping("selectOne/{id}")
    public Record selectByOne(@PathVariable("id") int id){
        return rs.getById(id);
    }
    @DeleteMapping("delete/{id}")
    public void delete(@PathVariable("id")int id){
        rs.removeById(id);
    }
}

